package com.example.familylibrary.service.impl;

import com.example.familylibrary.dto.ReadingNoteDTO;
import com.example.familylibrary.entity.ReadingNoteEntity;
import com.example.familylibrary.repository.ReadingNoteRepository;
import com.example.familylibrary.service.ReadingNoteService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReadingNoteServiceImpl implements ReadingNoteService {

    @Autowired
    private ReadingNoteRepository readingNoteRepository;

    @Override
    public ReadingNoteDTO createNote(ReadingNoteDTO readingNoteDTO) {
        ReadingNoteEntity noteEntity = new ReadingNoteEntity();
        BeanUtils.copyProperties(readingNoteDTO, noteEntity);
        
        ReadingNoteEntity savedNote = readingNoteRepository.save(noteEntity);
        
        ReadingNoteDTO result = new ReadingNoteDTO();
        BeanUtils.copyProperties(savedNote, result);
        return result;
    }

    @Override
    public ReadingNoteDTO getNoteById(Long id) {
        ReadingNoteEntity noteEntity = readingNoteRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("阅读想法记录不存在"));
        
        ReadingNoteDTO result = new ReadingNoteDTO();
        BeanUtils.copyProperties(noteEntity, result);
        return result;
    }

    @Override
    public List<ReadingNoteDTO> getNotesByUserAndBook(Long userId, Long bookId) {
        return readingNoteRepository.findByUserIdAndBookId(userId, bookId).stream().map(noteEntity -> {
            ReadingNoteDTO noteDTO = new ReadingNoteDTO();
            BeanUtils.copyProperties(noteEntity, noteDTO);
            return noteDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ReadingNoteDTO> getNotesByBookId(Long bookId) {
        return readingNoteRepository.findByBookId(bookId).stream().map(noteEntity -> {
            ReadingNoteDTO noteDTO = new ReadingNoteDTO();
            BeanUtils.copyProperties(noteEntity, noteDTO);
            return noteDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ReadingNoteDTO> getNotesByUserId(Long userId) {
        return readingNoteRepository.findByUserId(userId).stream().map(noteEntity -> {
            ReadingNoteDTO noteDTO = new ReadingNoteDTO();
            BeanUtils.copyProperties(noteEntity, noteDTO);
            return noteDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public ReadingNoteDTO updateNote(Long id, ReadingNoteDTO readingNoteDTO) {
        ReadingNoteEntity noteEntity = readingNoteRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("阅读想法记录不存在"));
        
        noteEntity.setPageNumber(readingNoteDTO.getPageNumber());
        noteEntity.setContent(readingNoteDTO.getContent());
        noteEntity.setHighlightText(readingNoteDTO.getHighlightText());
        noteEntity.setColor(readingNoteDTO.getColor());
        
        ReadingNoteEntity updatedNote = readingNoteRepository.save(noteEntity);
        
        ReadingNoteDTO result = new ReadingNoteDTO();
        BeanUtils.copyProperties(updatedNote, result);
        return result;
    }

    @Override
    public void deleteNote(Long id) {
        if (!readingNoteRepository.existsById(id)) {
            throw new RuntimeException("阅读想法记录不存在");
        }
        readingNoteRepository.deleteById(id);
    }
}